<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sent extends MY_Controller {

	//查看文章
	public function show_article(){
		
		//载入分页类
		$this->load->library('pagination');
		$perPage = 4;
//		echo 'hel';die;
		//分页配置
		$config['base_url'] = site_url('admin/sent/show_article');
		$config['total_rows'] = $this->db->count_all_results('article');
//		echo $config['total_rows'];die;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 4;
		$config['first_link'] = '第一页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$config['last_link'] = '最后一页';
		
		$this->pagination->initialize($config);
		
		$data['links'] = $this->pagination->create_links();
//		p($data);die;
		$offset = $this->uri->segment(4);
		$this->db->limit($perPage, $offset);
		//echo '这里是查看文章';
		$this->load->model('sent_model','arc');
		$data['article'] = $this->arc->article_find();
//		p($data);die;
		$this->load->view('admin/find_article.html',$data);
	}
	//发表文章模板显示
	public function article(){
		//调取栏目
		$this->load->model('Column_model','cate');
		$data['editor'] = $this->cate->model_show();
		//p($data);die;
		$this->load->helper('form');//载入辅助函数
		$this->load->view('admin/articles.html',$data);
	}
	//发表文章动作
	public function send(){
		// 表单验证
		
		$this->load->library('form_validation');//载入表单验证类


		// $this->form_validation->set_rules('title', '文章标题', 'required|min_length[5]');//载入规则
		// $this->form_validation->set_rules('type', '类型', 'required|integer');
		// $this->form_validation->set_rules('cid', '栏目', 'required|integer');
		// $this->form_validation->set_rules('info', '摘要', 'required|max_length[10]');
		// $this->form_validation->set_rules('content', '文章', 'required|max_length[2000]');

		$status = $this->form_validation->run('article');//执行规则
		if($status){
//文件上传------------------------
			//配置
			 $config['upload_path'] = './uploads/';
			//$config['upload_path'] = base_url().'uploads/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';			
			$config['max_size'] = '100000';
			$config['file_name'] = time() . mt_rand(1000,9999);
			
			//载入上传类
			$this->load->library('upload', $config);
			//执行上传
			$status = $this->upload->do_upload('thumb');
			
			$wrong = $this->upload->display_errors();
			
			if($wrong){
				error($wrong,'admin/sent/send');
			}
			//返回信息
			$info = $this->upload->data();
			// p($info);die;

			//缩略图-----------------
			//配置
			$arr['source_image'] = $info['full_path'];
			$arr['create_thumb'] = FALSE;
			$arr['maintain_ratio'] = TRUE;
			$arr['width'] = 200;
			$arr['height'] = 200;	

			//载入缩略图类
			$this->load->library('image_lib', $arr);
			//执行动作
			$status = $this->image_lib->resize();

			//var_dump($status);die;

			if(!$status){
				error('缩略图动作失败','admin/sent/send');
			}

		$this->load->model('sent_model', 'art');
		$data = array(
			'title' =>$this->input->post('title'),
			'type' =>$this->input->post('type'),
			'cid' =>$this->input->post('cid'),
			'thumb' =>$info['file_name'],
			'info' =>$this->input->post('info'),
			'content' =>$this->input->post('content'),
			'time' =>time() 
		 );
		    $this->art->article_add($data);
//		     p($data);die;	
			success('admin/sent/show_article', '发表成功');
		}else {			
			$this->load->helper('form');
			$this->load->view('admin/articles.html');
		}
	}

	// 编辑文章
	public function editor(){
			$this->load->helper('form');//载入辅助函数
			$this->load->view('admin/editor_article.html');
		}
	//编辑文章动作
	public function editor_verification(){
		
		$this->load->library('form_validation');
		$status = $this->form_validation->run('article');//执行规则
		if($status){
			echo '数据库操作';
		}else{
			$this->load->helper('form');//载入辅助函数
			$this->load->view('admin/editor_article.html');
		}
	}
}